<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Model\ChargeDetail;
use App\Model\ChargeOption;
use App\Model\Distributors;
use App\Model\MainBanner;
use Hyperf\Redis\Redis;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;
use RedisException;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use Util\Common;
use App\Controller\AbstractController;

class MainBannerController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected MainBanner $mainBanner;



    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getList(RequestInterface $request):ResponseInterface
    {
        $page = $request->inputs(['page','limit','type']);
        $data['list'] = $this->mainBanner->where(function ($query) use ($page) {
           $query->where('is_del',Common::DEL_SHOW);
           if($page['type']){
               $query->where('type',$page['type']);
           }
        })->orderBy('sort', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->get()->toArray();
        $data['total']= $this->mainBanner->where(function ($query) use ($page) {
            $query->where('is_del',Common::DEL_SHOW);
            if($page['type']){
                $query->where('type',$page['type']);
            }
        })->count();
        return $this->success($data);
    }


    /**创建
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function create(RequestInterface $request):\Psr\Http\Message\ResponseInterface
    {
        $data = $request->post();
        if(isset($data['id'])){
            $id = $data['id'];
            unset($data['id']);
        }
        if (isset($id) && $id) {
            $this->mainBanner->where('id', $id)->update($data);
        } else {
            MainBanner::create($data);
        }
        return $this->success();
    }

    /**删除
     * @param RequestInterface $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function del(RequestInterface $request):\Psr\Http\Message\ResponseInterface
    {
        try{
            $id = $request->input('id',0,'int');
            if (!$id) {
                return $this->error(ErrorCode::PARAMS_ERR);
            }
            $this->mainBanner->where('id',$id)->update(['is_del'=>Common::DEL_HIDE]);
        return $this->success();
        }catch (\Exception $e){
            return $this->error(code:1001,msg:'操作繁忙请稍后重试');
        }
    }

}
